package com.github.aloxc.plugin.restplus.method.action;

import com.github.aloxc.plugin.restplus.action.PushYapiAction;
import com.github.aloxc.plugin.restplus.common.PsiClassHelper;
import com.github.aloxc.plugin.restplus.common.PsiMethodHelper;
import com.intellij.codeInsight.daemon.GutterIconNavigationHandler;
import com.intellij.openapi.editor.ex.EditorGutterComponentEx;
import com.intellij.openapi.editor.impl.EditorImpl;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.ui.awt.RelativePoint;
import org.jetbrains.kotlin.asJava.LightClassUtilsKt;
import org.jetbrains.kotlin.psi.KtClass;
import org.jetbrains.kotlin.psi.KtNamedFunction;

import java.awt.event.MouseEvent;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 在文件内容左边的竖栏上显示运行按钮的处理方法
 */
public class PushYapiLineMarkerHandler implements GutterIconNavigationHandler {
    @Override
    public void navigate(MouseEvent mouseEvent, PsiElement psiElement) {
        try {
//            PopupBalloon.showPopupBalloon(new RelativePoint(mouseEvent), I18Bundle.getMessage("com.github.aloxc.plugin.restplus.EditorPopupMenu.run.usercase.noinit.text"), false);
            PsiMethod psiMethod = null;
            PsiClass psiClass = null;
            if (psiElement instanceof PsiMethod) {
                psiMethod = (PsiMethod) psiElement;
            } else if (psiElement instanceof KtNamedFunction) {
                psiMethod = LightClassUtilsKt.toLightMethods(psiElement).get(0);
            }else if (psiElement instanceof PsiClass) {
                psiClass = (PsiClass) psiElement;
            } else if (psiElement instanceof KtClass) {
                KtClass ktClass = (KtClass) psiElement;
                psiClass = LightClassUtilsKt.toLightClass(ktClass);
            }
            Project project = null;
            if(psiMethod != null){
                project = psiMethod.getProject();
            }else{
                project = psiClass.getProject();
            }
            Object editorGutterImpl = (EditorGutterComponentEx) mouseEvent.getSource();
            Field field = null;
            Field[] declaredFields = editorGutterImpl.getClass().getDeclaredFields();
            for (int i = 0; i < declaredFields.length; i++) {
                field = declaredFields[i];
                if (field.getType().getName().equals(EditorImpl.class.getName())) {
                    break;
                }
            }
            PsiElement curElement = psiClass;
            if(curElement == null){
                curElement = psiMethod;
            }
            field.setAccessible(true);
            EditorImpl editor = (EditorImpl) field.get(editorGutterImpl);
            VirtualFile file = FileDocumentManager.getInstance().getFile(editor.getDocument());
            String filePath = file.getPath();
            Module[] modules = ModuleManager.getInstance(project).getModules();
            List<Module> curModuleList = Arrays.stream(modules).filter(module -> {
                String modulePath = module.getModuleFile().getParent().getPath();
                return filePath.contains(modulePath);
            }).sorted((m1, m2) ->
                    m2.getModuleFile().getParent().getPath().length() - m1.getModuleFile().getParent().getPath().length()
            ).limit(1).collect(Collectors.toList());

            PushYapiAction pushYapiAction = new PushYapiAction();
            pushYapiAction.runIt(project,curElement,file);
        } catch (Exception ex) {

        }
    }
}
